from ..async_tcp_server import serverInQueue, serverOutQueue
import asyncio


class DoorControl:

    def __init__(self):
        self.__openStatus: str = '关闭'
        self.__inputPassWord: str = ''
        self.__keyPassWord: str = '20010428'

    def getOpenStatus(self):
        return self.__openStatus

    def getInputPassWord(self):
        return self.__inputPassWord

    async def setOpenStatus(self, status: str):
        self.__openStatus = status
        cmd = self.__changeCMD()
        await self.__sendCMD(cmd)

    def setInputPassWord(self, single_pass: str):
        self.__inputPassWord += single_pass

    def delInputPassWord(self):
        passList = list(self.__inputPassWord)
        try:
            passList.pop()
        except IndexError:
            pass
        self.__inputPassWord = ''.join(passList)

    def getKeyPassWord(self):
        return self.__keyPassWord

    def setKeyPassWord(self, password: str):
        if len(password) != 8:
            return False
        else:
            self.__keyPassWord = password
            return True

    def __changeCMD(self):
        signal = '1' if self.__openStatus == '开启' else '0'
        return f"D={signal};"

    async def __sendCMD(self, cmd):
        serverInQueue.put(cmd)
        res_status = "F"
        while res_status != 'T':
            if not serverOutQueue.empty():
                res_status = serverOutQueue.get()
            else:
                await asyncio.sleep(0)